package com.spring.controller.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public  class FilterController extends AbstractController  {

	public static final String LOGIN_PAGE_VIEW ="login/login";
	
	private boolean checkAdminLogged(HttpServletRequest request){
		logger.debug(">>>>>>> preHandle() ");
		HttpSession session = request.getSession();
		
		if (session.getAttribute("admin") !=null ) {
			logger.debug(">> admin logged: proced with orginal request. ");
			return true;
		} 
		
		logger.debug(">> admin NOT logged: calling login Page. ");
		return false;
	}
	
	public ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		logger.debug(">> filter >>>>>>>>> START ");
		
		String urlCalled = request.getServletPath();
		
		
		
		ModelAndView mav = new ModelAndView("login/login");
		
		
		//if(checkAdminLogged(request) ){
			logger.debug(">> admin found >> go to original request");
			
			//mav = new ModelAndView("index");
			mav = new ModelAndView("forward:"+urlCalled);
		//}else{
			//mav = new ModelAndView("login/login");
//		}
		return mav;
	} 
}
